<template>
  <div class="loan-container">
    <el-card class="page-header-card">
      <h2 class="page-title">部门人员借调</h2>
      <p class="page-description">请填写人员借调信息</p>
    </el-card>

    <el-card class="form-card">
      <el-form
        ref="formRef"
        :model="form"
        label-width="120px"
        :rules="rules"
        class="custom-form"
      >
        <el-form-item label="原学工号" prop="henau_number">
          <el-input
            v-model="form.henau_number"
            placeholder="请输入原学工号"
            class="custom-input"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入姓名"
            class="custom-input"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="新学工号" prop="new_henau_number">
          <el-input
            v-model="form.new_henau_number"
            placeholder="请输入新学工号"
            class="custom-input"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="部门" prop="department_code">
          <el-select
            v-model="form.department_code"
            filterable
            placeholder="请选择部门"
            class="custom-input"
            @change="handleDepartmentChange"
          >
            <el-option
              v-for="item in departmentOptions"
              :key="item.oid"
              :label="item.zh_name"
              :value="item.oid"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="科室" prop="office_code">
          <el-select
            v-model="form.office_code"
            filterable
            placeholder="请选择科室"
            class="custom-input"
            :disabled="!form.department_code"
          >
            <el-option
              v-for="item in officeOptions"
              :key="item.oid"
              :label="item.zh_name"
              :value="item.oid"
            />
          </el-select>
        </el-form-item>

        <el-form-item class="form-buttons">
          <el-button
            type="primary"
            :icon="Check"
            class="custom-button"
            @click="submitForm"
          >
            确认提交
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { User, Check } from "@element-plus/icons-vue";
import { Secondment, getAllInstitution } from "@/api";

const formRef = ref();
const departmentOptions = ref([]);
const officeOptions = ref([]);
const institutionData = ref([]);

const form = reactive({
  henau_number: "",
  name: "",
  new_henau_number: "",
  department_code: "",
  office_code: ""
});

// 表单验证规则
const rules = {
  henau_number: [
    { required: true, message: "请输入原学工号", trigger: "blur" }
  ],
  name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
  new_henau_number: [
    { required: true, message: "请输入新学工号", trigger: "blur" }
  ],
  department_code: [
    { required: true, message: "请选择部门", trigger: "change" }
  ],
  office_code: [{ required: true, message: "请选择科室", trigger: "change" }]
};

// 获取部门科室数据
const getInstitutionData = async () => {
  try {
    const res = await getAllInstitution();
    if (res.status === "success") {
      institutionData.value = res.data;
      // 筛选出所有部门（class_id为3的数据）
      departmentOptions.value = res.data.filter(item => item.class_id === 3);
    }
  } catch (error) {
    ElMessage.error("获取部门数据失败");
  }
};

// 部门选择变化时更新科室选项
const handleDepartmentChange = value => {
  form.office_code = ""; // 清空科室选择
  // 筛选出当前选中部门下的所有科室
  officeOptions.value = institutionData.value.filter(
    item => item.class_id === 4 && item.pid === value
  );
};

const submitForm = async () => {
  if (!formRef.value) return;

  try {
    await formRef.value.validate();
    await Secondment(form);
    ElMessage.success("提交成功");
    // 重置表单
    formRef.value.resetFields();
  } catch (error) {
    ElMessage.error("提交失败，请检查表单信息");
  }
};

onMounted(() => {
  getInstitutionData();
});
</script>

<style scoped>
.loan-container {
  min-height: calc(100vh - 80px);
  padding: 40px;
}

.page-header-card {
  margin-bottom: 20px;
  text-align: center;
}

.page-title {
  margin: 0 0 12px;
  font-size: 28px;
  font-weight: 600;
  color: #2c3e50;
  letter-spacing: 1px;
}

.page-description {
  margin: 0;
  font-size: 16px;
  color: #5e6d82;
}

.form-card {
  max-width: 600px;
  margin: 0 auto;
}

.custom-form {
  margin-top: 20px;
}

.custom-input {
  width: 100%;
  transition: all 0.3s ease;
}

.form-buttons {
  margin-top: 40px;
}

.form-buttons :deep(.el-form-item__content) {
  justify-content: center;
  margin-left: 0 !important;
}

.custom-button {
  padding: 12px 30px;
  font-size: 14px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #2c3e50;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 0 0 1px #dcdfe6 inset;
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #409eff inset;
}

:deep(.el-input__wrapper:focus-within) {
  box-shadow: 0 0 0 2px rgb(64 158 255 / 20%) inset;
}
</style>
